program principal
interface

subroutine dicotomia(a,b,it,tol)
	real(kind=8)::tol,a,b,x,ant
	integer::it,i
end subroutine
function funcion(x)result(res1)
	real(Kind=8),intent(in)::x
	real(kind=8)::res1

end function

subroutine datos(a,b,maxit,tol)
	real(kind=8)::tol,a,b
	integer::maxit
end subroutine

end interface

real(kind=8)::a,b,tol
integer::maxit
	call datos(a,b,maxit,tol)
	call dicotomia(a,b,maxit,tol)



end program


recursive subroutine datos(a,b,maxit,tol)
	real(kind=8)::tol,a,b
	integer::maxit

		print*,'¿cual es el intervalo de partida?'
		read(5,*)a
		read(5,*)b
		if(funcion(a)==0._8) then 
			print*,'la raiz es: ',a
			stop('')
		else	if (funcion(b)==0._8) then 
			print*,'la raiz es: ',b
			stop
		end if

		if((funcion(a)*funcion(b))>0._8) then
			print*,'el intervalo no es valido,introduzca otro'
			call datos(a,b,maxit,tol)
		else
			print*,'¿numero maximo de iteraciones?'
			read(5,*)maxit
			print*,'¿tolerancia de error?'
			read(5,*)tol
		end if
	

	
end subroutine



function funcion(x)result(res1)
	real(Kind=8),intent(in)::x
	real(kind=8)::res1

	res1=30._8-x*10._8
end function

subroutine dicotomia(a,b,it,tol)
	real(kind=8)::tol,a,b,x,ant
	integer::it,i
	interface
		function funcion(x)result(res1)
			real(Kind=8),intent(in)::x
			real(kind=8)::res1
		end function
	end interface
	open(unit=1, file='integracion.dat',status='unknown')
	x=a
	do i=1,it,1
		ant=x
		x=(a+b)*0.5_8
		
		if(funcion(a)*funcion(x)<0) then
			b=x
		else
			a=x
		end if
		error=abs((x-ant)/x)

		write(1,*)'iteracion: ',i
		write(1,*)'Aproximacion'
		write(1,'(f17.14)'),x
		write(1,*)'error relativo: '
		write(1,'(e21.14)')error
		write(1,*)
		if(funcion(x)==0._8 .or. error<tol) then
			exit
		end if
	end do
	close(1)
end subroutine










